// 设置文件
import setting from '@/setting.js'

export default {
  namespaced: true,
  state: {
    // 顶栏菜单
    header: [],
    // 侧栏菜单
    aside: [],
    top: null,
    // 侧边栏收缩
    asideCollapse: setting.menu.asideCollapse,
    // 侧边栏折叠动画
    asideTransition: setting.menu.asideTransition,
    // 标题
    headline: ''

  },
  getters: {
    /**
     * @description 获取联动的侧边栏
     * @param {Object} state state
     */
    asideMenus(state) {
      let temp = state.aside.filter(menu => menu.name === state.top)
      if (temp.length === 0) {
        return []
      }
      if (!temp[0].children) {
        return temp
      }
      return temp[0].children
    },

    getHeadline(state) {
      return state.headline
    }
  },
  actions: {
    /**
     * 设置侧边栏展开或者收缩
     * @param {Object} context
     * @param {Boolean} collapse is collapse
     */
    async asideCollapseSet({
      state,
      dispatch
    }, collapse) {
      // store 赋值
      state.asideCollapse = collapse
      // 持久化
      await dispatch('db/set', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        value: state.asideCollapse,
        user: true
      }, {
        root: true
      })
    },
    /**
     * 切换侧边栏展开和收缩
     * @param {Object} context
     */
    async asideCollapseToggle({
      state,
      dispatch
    }) {
      // store 赋值
      state.asideCollapse = !state.asideCollapse
      // 持久化
      await dispatch('db/set', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        value: state.asideCollapse,
        user: true
      }, {
        root: true
      })
    },
    /**
     * 设置侧边栏折叠动画
     * @param {Object} context
     * @param {Boolean} transition is transition
     */
    async asideTransitionSet({
      state,
      dispatch
    }, transition) {
      // store 赋值
      state.asideTransition = transition
      // 持久化
      await dispatch('db/set', {
        dbName: 'sys',
        path: 'menu.asideTransition',
        value: state.asideTransition,
        user: true
      }, {
        root: true
      })
    },
    /**
     * 切换侧边栏折叠动画
     * @param {Object} context
     */
    async asideTransitionToggle({
      state,
      dispatch
    }) {
      // store 赋值
      state.asideTransition = !state.asideTransition
      // 持久化
      await dispatch('db/set', {
        dbName: 'sys',
        path: 'menu.asideTransition',
        value: state.asideTransition,
        user: true
      }, {
        root: true
      })
    },
    /**
     * 持久化数据加载侧边栏设置
     * @param {Object} context
     */
    async asideLoad({
      state,
      dispatch
    }) {
      // store 赋值
      const menu = await dispatch('db/get', {
        dbName: 'sys',
        path: 'menu',
        defaultValue: setting.menu,
        user: true
      }, {
        root: true
      })
      state.asideCollapse = menu.asideCollapse !== undefined ? menu.asideCollapse : setting.menu.asideCollapse
      state.asideTransition = menu.asideTransition !== undefined ? menu.asideTransition : setting.menu.asideTransition
    }
  },
  mutations: {
    /**
     * @description 设置顶栏菜单
     * @param {Object} state state
     * @param {Array} menu menu setting
     */
    headerSet(state, menu) {
      // store 赋值
      state.header = menu
    },
    /**
     * @description 设置侧边栏菜单
     * @param {Object} state state
     * @param {Array} menu menu setting
     */
    asideSet(state, menu) {
      // store 赋值
      state.aside = menu
    },

    /**
     * @description 设置顶部的菜单
     * @param {Object} state state
     * @param {String} top top
     */
    topSet(state, top) {
      state.top = top
    },

    headlineSet(state, headline) {
      state.headline = headline
    }
  }
}
